package common.library.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.widget.ImageView;

import java.io.InputStream;

import androidx.appcompat.app.AppCompatActivity;
import lombok.var;

public class ImageUtil {

    public static Bitmap get(String path) {
        return BitmapFactory.decodeFile(path);
    }

    public static Bitmap get(InputStream stream) {
        var bitmap = BitmapFactory.decodeStream(stream);
        try {
            stream.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return bitmap;
    }

    public static Bitmap get(AppCompatActivity activity, int id) {
        return BitmapFactory.decodeResource(activity.getResources(), id);
    }

    public static Bitmap cut(Bitmap source, int x, int y, int width, int height) {
        return Bitmap.createBitmap(source, x, y, width, height);
    }

    /**
     * 按比例切图
     */
    public static Bitmap cut(Bitmap source, float keep, boolean foursquare) {
        var width = source.getWidth();
        var height = source.getHeight();

        var x = (int) ((1 - keep) / 2 * width);
        var y = (int) ((1 - keep) / 2 * height);
        if (x < 3 || y < 3)
            return source;

        width = (int) (width * keep);
        height = (int) (height * keep);

        //保留正方形？用于二维码扫描
        if (foursquare) {
            if (width > height)
                width = height;
            else
                height = width;
        }

        return Bitmap.createBitmap(source, x, y, width, height);
    }

    /**
     * 黑白化
     */
    public static Bitmap gray(Bitmap source) {
        Bitmap bitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setSaturation(0);
        ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(colorMatrix);
        paint.setColorFilter(colorMatrixColorFilter);
        canvas.drawBitmap(source, 0, 0, paint);
        return bitmap;
    }

    public static Bitmap scale(Bitmap source, float scale) {
        return Bitmap.createScaledBitmap(source, (int) (source.getWidth() * scale), (int) (source.getHeight() * scale), false);
    }

    public static void set(AppCompatActivity activity, final ImageView imageView, final Bitmap bitmap) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                imageView.setImageBitmap(bitmap);
            }
        });
    }

    public static void set(final AppCompatActivity activity, final int imageViewId, final Bitmap bitmap) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ImageView imageView = activity.findViewById(imageViewId);
                imageView.setImageBitmap(bitmap);
            }
        });
    }
}
